<?php
abstract class Renderable {
	
	protected $elementId;
	private $dataTheme;
	
	private $parent;
	
	private $attributeArray = array();
	
	public function __construct($elementId) {
		
		$this->elementId = $elementId;
		$this->dataTheme = NULL;
	}
	
	public function addAttribute(Attribute $attribute) {
		
		$this->attributeArray[] = $attribute;
	}
	
	public function setDataTheme(DataTheme $dataTheme) {
		
		$this->dataTheme = $dataTheme;
	}
	
	protected function getDataTheme() {
		
		return $this->dataTheme;
	}
	
	protected function setParent(Renderable $parent) {
		
		$this->parent = $parent;
	}
	
	protected function formatAttributes() {
		
		$formattedAttributes = ' ';
		
		foreach ($this->attributeArray as $attribute) {
			$formattedAttributes = $formattedAttributes . $attribute->format() . ' ';
		}
		
		return $formattedAttributes;
	}
	
	protected function preRender() {
		
		if ($this->elementId != NULL) {
			$this->addAttribute(new ID($this->elementId));
		}
		
		if ($this->parent != NULL &&
			$this->parent->getDataTheme() != NULL &&
			$this->dataTheme == NULL) {
			$this->dataTheme = $this->parent->getDataTheme();
		}
		
		if ($this->dataTheme != NULL) {
			$this->addAttribute($this->dataTheme);
		}
	}
	
	protected abstract function renderElement();
	
	public function render() {
		
		$this->preRender();
		$this->renderElement();
	}
}
?>